package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eic.framework.core.service.ServicePlusImpl;
import com.eic.framework.spring.DelayInitUtils;
import com.ruoyi.common.core.domain.entity.DictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.redis.RedisSubscriber;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.dao.SysDictData;
import com.ruoyi.system.domain.dao.SysDictType;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.system.mapper.SysDictTypeMapper;
import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.Serializable;
import java.util.*;

/**
 * 字典 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysDictDataServiceImpl extends ServicePlusImpl<SysDictDataMapper, SysDictData> implements ISysDictDataService, RedisSubscriber
{
    private String channel="dictData";
    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysDictTypeMapper dictTypeMapper;
    @Autowired
    private RedisCache redisCache;

    public SysDictDataServiceImpl(){
        RedisCache.registerSubscriber(channel,this);
    }

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init()
    {
        DelayInitUtils.add(5, new Runnable() {
            @Override
            public void run() {
                loadingDictCache();
            }
        });
    }

    @Override
    public void onMessage(String message) {
        loadingDictCache();
    }

    /**
     * 加载字典缓存数据
     */
    @Override
    public void loadingDictCache()
    {
        Map<String, DictData> dataMap=new HashMap<>();
        Map<String, List<DictData>> typeMap=new HashMap<>();
        Map<String, String> typeIdMap=new HashMap<>();
        List<SysDictType> typeList=dictTypeMapper.selectList(null);
        List<SysDictData> dataList=super.list(Wrappers.<SysDictData>lambdaQuery().orderByAsc(SysDictData::getDictSort));
        for(SysDictType type:typeList){
            typeIdMap.put(type.getDictId(),type.getDictType());
            typeMap.put(type.getDictType(),new ArrayList<>());
        }
        for(SysDictData sysData:dataList){
            String dictType=typeIdMap.get(sysData.getTypeId());
            if(dictType!=null){
                List<DictData> typeDataList=typeMap.get(dictType);
                DictData dictData=new DictData();
                dictData.setDictValue(sysData.getDictValue());
                dictData.setDictLabel(sysData.getDictLabel());
                dictData.setDictSort(sysData.getDictSort());
                dictData.setCssClass(sysData.getCssClass());
                dictData.setListClass(sysData.getListClass());
                dictData.setIsDefault(sysData.getIsDefault());
                if(sysData.getStatus()==null || sysData.getStatus().equals("0")){
                    typeDataList.add(dictData);
                }
                dataMap.put(dictType+"#"+dictData.getDictValue(),dictData);
            }
        }
        DictUtils.setDictCache(dataMap,typeMap);
    }

    @Override
    public boolean insert(SysDictData entity) {
        redisCache.publish(channel,"refresh");
        return super.insert(entity);
    }

    @Override
    public boolean updateById(SysDictData entity) {
        redisCache.publish(channel,"refresh");
        return super.updateById(entity);
    }

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        redisCache.publish(channel,"refresh");
        return super.removeByIds(idList);
    }

}
